package com.gollum.web.security.handler;

import javax.servlet.http.HttpServletRequest;

import com.gollum.common.domain.response.RestResponse;
import com.gollum.common.exception.BaseException;
import com.gollum.common.exception.ServiceException;
import com.gollum.common.exception.UnAuthorizationException;
import com.gollum.common.utils.MessageUtils;
import com.gollum.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 * 
 * @author ruoyi
 */
@RestControllerAdvice
public class GlobalExceptionHandler
{
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(BaseException.class)
    public RestResponse handleBaseException(BaseException e)
    {
        log.error("通用异常抛出错误消息为：[{}]", e.getMessage());
        return RestResponse.fail(e.getMessage());
    }

    /**
     * 权限校验异常
     */
    @ExceptionHandler(AccessDeniedException.class)
    public RestResponse handleAccessDeniedException(AccessDeniedException e, HttpServletRequest request)
    {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',权限校验失败'{}'", requestURI, e.getMessage());
        return RestResponse.fail("没有权限，请联系管理员授权");
    }

    /**
     * 请求方式不支持
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public RestResponse handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
            HttpServletRequest request)
    {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
        return RestResponse.fail(e.getMessage());
    }

    /**
     * 业务异常
     */
    @ExceptionHandler(ServiceException.class)
    public RestResponse handleServiceException(ServiceException e, HttpServletRequest request)
    {
        log.error(e.getMessage(), e);
        Integer code = e.getCode();
        return StringUtils.isNotNull(code) ? RestResponse.fail(code, e.getMessage()) : RestResponse.fail(e.getMessage());
    }

    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public RestResponse handleRuntimeException(RuntimeException e, HttpServletRequest request)
    {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生未知异常.", requestURI, e);
        return RestResponse.fail(e.getMessage());
    }

    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public RestResponse handleException(Exception e, HttpServletRequest request)
    {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生系统异常.", requestURI, e);
        return RestResponse.fail(e.getMessage());
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(BindException.class)
    public RestResponse handleBindException(BindException e)
    {
        log.error(e.getMessage(), e);
        String message = e.getAllErrors().get(0).getDefaultMessage();
        return RestResponse.fail(message);
    }

    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public RestResponse handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e)
    {
        log.error(e.getMessage(), e);
        return RestResponse.fail("参数类型不符合");
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public RestResponse handleMethodArgumentNotValidException(MethodArgumentNotValidException e)
    {
        log.error(e.getMessage(), e);
        String message = e.getBindingResult().getFieldError().getDefaultMessage();
        return RestResponse.fail(message);
    }

    @ExceptionHandler(UnAuthorizationException.class)
    public RestResponse handleUnAuthorizationException(UnAuthorizationException e){
        log.error(e.getMessage());
        return RestResponse.fail("登录过期");
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public RestResponse handleHttpMessageNotReadableException(HttpMessageNotReadableException e)
    {
        log.error(e.getMessage(), e);
        return RestResponse.fail("请求参数有误");
    }
    ///**
    // * 演示模式异常
    // */
    //@ExceptionHandler(DemoModeException.class)
    //public AjaxResult handleDemoModeException(DemoModeException e)
    //{
    //    return AjaxResult.error("演示模式，不允许操作");
    //}
}
